package com.tanhua.dubbo.api.mongo.impl;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.api.mongo.TestSoulApi;
import com.tanhua.model.domain.User;
import com.tanhua.model.mongo.TestReport;
import com.tanhua.model.mongo.TestSoul;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class TestSoulApiImpl implements TestSoulApi {
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void save(List<User> userList) {
        for (User user : userList) {
            Integer flag = 1;
            for (int i = 1; i <= 3; i++) {
                TestSoul testSoul = new TestSoul();
                testSoul.setUserId(user.getId());
                testSoul.setQuestionnaireId(flag.longValue());
                if (flag == 1) {
                    testSoul.setIsLock(0);
                } else {
                    testSoul.setIsLock(1);
                }
                mongoTemplate.save(testSoul);
                flag++;
            }
        }
    }

    @Override
    public void saveTest(User user) {
        Integer flag = 1;
        for (int i = 1; i <= 3; i++) {
            TestSoul testSoul = new TestSoul();
            testSoul.setUserId(user.getId());
            testSoul.setQuestionnaireId(flag.longValue());
            if (flag == 1) {
                testSoul.setIsLock(0);
            } else {
                testSoul.setIsLock(1);
            }
            mongoTemplate.save(testSoul);
            flag++;
        }
    }

    @Override
    public List<TestSoul> selectByUserId(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<TestSoul> testSoulList = mongoTemplate.find(query, TestSoul.class);
        return testSoulList;
    }

    //根据当前用户提交的问卷id，解锁下一个问卷
    @Override
    public void unlockNext(Long id, Long questionnaireId) {
        Long nextQuestionnaireId = 2l;   //默认下一个问卷为2

        if (questionnaireId == 2) {
            nextQuestionnaireId = 3l;
        } else if (questionnaireId == 3) {
            return;
        }
        Query query = new Query(Criteria.where("userId").is(id).and("questionnaireId").is(nextQuestionnaireId));
        Update update = new Update();
        update.set("isLock", 0);  //解锁该问卷
        mongoTemplate.updateFirst(query, update, TestSoul.class);
    }

    //根据报告id获得报告
    @Override
    public TestReport findByReportId(String reportId) {
        Query query = new Query(Criteria.where("id").is(new ObjectId(reportId)));
        TestReport testReport = mongoTemplate.findOne(query, TestReport.class);
        return testReport;
    }

    @Override
    public List<Long> getSimilarUsers(Long userId, Integer score) {
        Criteria criteria = new Criteria();
        criteria.and("score").gt(score - 5).lt(score + 5);
        criteria.and("userId").ne(userId);
        Query query = new Query(criteria);
        List<TestReport> testReports = mongoTemplate.find(query, TestReport.class);
        List<Long> userIds = CollUtil.getFieldValues(testReports, "userId", Long.class);
        return userIds;
    }

    @Override
    public void saveReport(TestReport testReport) {
        Query query = new Query(Criteria.where("userId").is(testReport.getUserId()).and("questionnaireId").is(testReport.getQuestionnaireId()));
        TestReport one = mongoTemplate.findOne(query, TestReport.class);
        if (one == null) {
            mongoTemplate.save(testReport);
        } else {
//            Update update = new Update();
//            update.set("_id",testReport.getId());
//            update.set("resultId", testReport.getResultId());
//            update.set("score", testReport.getScore());
//            mongoTemplate.updateFirst(query, update, TestReport.class);
            mongoTemplate.remove(query, TestReport.class);
            mongoTemplate.save(testReport);
        }
    }
}
